// JavaScript Document




var Comment = (function(){
    var comment = {};
	comment.Msg = ["不满意", "一般", "还不错", "满意", "十分满意"];
	comment.getList = function(pageindex){
		var self = this;
		$.ajax({
			url: "/comment/front/unvaluatedproduct/"+pageindex+".do",
			type: 'GET',
			dataType:"json",
			//jsonp:"jsonpcallback",
			data:{
				memberid: passport.cookie.memberId || "a6f299a4-2954-4843-8a8e-59c12d317563",
				plat: myplat,
				orderno: T.getQueryString("orderno") || ""
			},
			success: function(db){
				if(db.data.olist.length<=0){
					var noMsg = $('<li style="text-align: center;padding: 20px;color: #999;}"><span>没有未评价的商品</span></li>')
					$("#eavluation-list ul").empty().append(noMsg);
					return
				}
				$("#eavluation-list ul").empty();
				$("#eavluation-list ul").html($("#eavluation-list-tmpl").tmpl(db.data));	
				var _gcode = T.getQueryString("gcode")
				if(_gcode){
					$("#eavluation-list ul").find(".gcode"+_gcode).show();
				}else{
					$("#eavluation-list ul").find("li").eq(0).find(".comment-wrap").show();
				}		
				comment.makeCommentDown();
				var datacount = db.data.countNum; 
				$("#page_number").PagerBind({//更新分页
					pageindex: pageindex,
					datacount: datacount,
					databind : function(i){
						self.getList(i);
					}
				});
			}
		});
	}
	comment.makeCommentDown = function(){
		$(".stars-group").each(function(){
            var _canHover = false;
            
            $(this).find("i").hover(function(){
                var _index = $(this).index();
                var _i = _index + 1;
                $(this).parent().find("i:lt("+_i +")").addClass("hover");
                $(this).parent().find("i:gt("+_index +")").removeClass("hover");
                $(this).parent().find("span").html(comment.Msg[_index]).show();
            },function(){
                $(this).parent().find("i").removeClass("hover");
                if(!_canHover){
                    $(this).parent().find("span").html("").hide();
                }else{
                    var _length = $(this).parent().find("i.on").length - 1;
                    $(this).parent().find("span").html(comment.Msg[_length]);
                }
            }).click(function(){
                _canHover = true;

                var _index = $(this).index();
                var _i = _index + 1;
				$(this).parent().attr("data-escore",_i);
                $(this).parent().find("i:lt("+_i +")").addClass("on");
                $(this).parent().find("i:gt("+_index +")").removeClass("on");
                $(this).parent().find("span").html(comment.Msg[_index]).show();

                //显示评论框
                $(this).parents(".comt-list-main").find(".comment-wrap").stop().slideDown();
                $(this).parents(".comt-list-main").siblings().find(".comment-wrap").stop().slideUp();
            });
        });
	}
	comment.ajaxFileUpload = function(fid) {//图片上传		
		var thisImgBox = $("#"+fid).parents(".comment-wrap");
		var imgLength = thisImgBox.find(".user_img_list").length;
		if(imgLength>=10){alert("您最多只能上传10张图片。");return;}
		var orderno = $("#"+fid).attr("data-orderid");
		var gcode = $("#"+fid).attr("data-gcode");
		var loading = $("<span style='display:block; background:#fff;position: absolute;top: 0;'><img src='images/loading.gif' width='24' height='24'/></span>");
		$("#"+fid).parent("div").append(loading);
		$.ajaxFileUpload({
			url: "/comment/upload/?memberid="+passport.cookie.memberId+"&orderno="+orderno+"&gcode="+gcode, //用于文件上传的服务器端请求地址
			secureuri: false, //一般设置为false
			fileElementId: fid, //文件上传空间的id属性  <input type="file" id="file" name="file" />
			dataType: 'json', //返回值类型 一般设置为json
			type: "post",
			success: function (db)  //服务器成功响应处理函数
			{
				if(db.code != "000000"){}
				if(db.code == "130007"){alert(db.mag); return;}				
				var thisDd = $(
					'<dd>'+
						'<img src="'+db.data.minImgUrl+'" data-picId="'+db.data.id+'"/> '+
						'<span class="remove">点击删除</span>'+
					'</dd>'
				)
				thisImgBox.find("dl.user_img_list").append(thisDd);
				loading.remove();
			},
			error: function (db)//服务器响应失败处理函数
			{
				//loading.remove();
			}
		});		
		return false;
	}
	comment.postMycomment = function(info){
		$.ajax({
			url: "/comment/front/addvaluate.do",
			type: 'POST',
			dataType:"json",
			//jsonp:"jsonpcallback",
			data:{
				lenovoId: passport.cookie.lenovoId || "",
				memberid: passport.cookie.memberId || "a6f299a4-2954-4843-8a8e-59c12d317563", //"5e11cc62-bb9a-4e55-b964-d7b229b7b258",//用户id  String  必填         
				cuser: passport.cookie.loginName || "",   //"13717731384",//用户昵称  String  必填
				plat:myplat,                              //来源  String  必填
				orderno:info.orderno,//"47212",           //订单码              String     必填
				gcode:info.gcode,   //"51390",            //商品编号            String     必填
				isanonymous:info.isanonymous,               //是否匿名            String     非必须（只能为"0"或"1",0为默认不匿名，1为匿名）			
				picId:info.picId, //"ba85dcc8-44ac-455c-ba7a-80aed142e606",            //图片的id            String     非必填，多个需用,分开 					
				edesc:info.edesc,//                       //评论内容            String     必填
				escore:info.escore                        //评分                int        必填（现改为统一5分制）""
			},
			success: function (db) {
			    if (db.code == "000000") {
			        alert("评价成功");
			        comment.getList(1);
			    }
			    else {
			        alert(db.msg)
			    }
			}
		});
	}
	comment.bindEvent = function(){
		$("#eavluation-list").delegate(".comment-btns a","click",function(){//提交按钮事件绑定
			var info = {}
			var thisBox = $(this).parents("li");
			info.edesc = thisBox.find(".commentarea textarea").val();
			info.escore = thisBox.find(".stars-group").attr("data-escore");
			if($.trim(info.edesc) == ""){alert("您还没有写评价。"); return}
			if(!info.escore){alert("您还没有评分。"); return}
			
			info.orderno = $(this).attr("data-orderid");
			info.gcode = $(this).attr("data-gcode");
			info.isanonymous = thisBox.find(".checkbox_nm").prop("checked") == true ? 1:0;
			info.picId = (function(){
				var id = "";
				thisBox.find(".user_img_list img").each(function(){
					id += $(this).attr("data-picid")+",";
				})
				return id
			})();			
			comment.postMycomment(info);
		})
		$("#eavluation-list").delegate(".user_img_list dd","click",function(){//提交按钮事件绑定
			$(this).remove();
		})
	}
	comment.getUserInfo = function(){
		$(".user_logo").find("p").text(passport.cookie.loginName);
		$.ajax({
			url: "/comment/front/getPhoto.do?lenovoid="+ passport.cookie.lenovoId + "&plat=" + myplat,
			type: 'GET',
			dataType:"json",
			//jsonp:"jsonpcallback",			
			success: function(db){
				if(db.ret == 0){
					$("#userImg").attr("src",db.bigimg);
				}else{
					$("#userImg").attr("src","images/userlogo.jpg");
				}				
			}
		});
	}
	return comment;
})(jQuery, Comment)


$(function(){
	Comment.getList(1);
	Comment.bindEvent();
	Comment.getUserInfo();
})
















/*var commentMsg = ["不满意", "一般", "还不错", "满意", "十分满意"];
function comment(){
	$(".stars-group").each(function(){
		var _canHover = false;
		
		$(this).find("i").hover(function(){
			var _index = $(this).index();
			var _i = _index + 1;
			$(this).parent().find("i:lt("+_i +")").addClass("hover");
			$(this).parent().find("i:gt("+_index +")").removeClass("hover");
			$(this).parent().find("span").html(commentMsg[_index]).show();
		},function(){
			$(this).parent().find("i").removeClass("hover");
			if(!_canHover){
				$(this).parent().find("span").html("").hide();
			}else{
				var _length = $(this).parent().find("i.on").length - 1;
				$(this).parent().find("span").html(commentMsg[_length]);
			}
		}).click(function(){
			_canHover = true;

			var _index = $(this).index();
			var _i = _index + 1;
			$(this).parent().find("i:lt("+_i +")").addClass("on");
			$(this).parent().find("i:gt("+_index +")").removeClass("on");
			$(this).parent().find("span").html(commentMsg[_index]).show();

			//显示评论框
			$(this).parents(".comt-list-main").find(".comment-wrap").stop().slideDown();
			$(this).parents(".comt-list-main").siblings().find(".comment-wrap").stop().slideUp();
		});
	});
}*/